package main

import "fmt"

func main() {
	s := "RLLLLRRRLR"
	fmt.Println(balancedStringSplit(s))
}

//#link:https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/
func balancedStringSplit(s string) int {
	h := newHeap()
	first, result := s[0], 0
	for _, c := range []byte(s) {
		if len(h.value) == 0 {
			result++
			first = c
		} else {
			if h.peak() != c {
				h.pop()
			}
		}
		if c == first {
			h.push(c)
		}
	}
	return result
}

type heap struct {
	value []byte
}

func newHeap() *heap {
	return &heap{
		value: make([]byte, 0),
	}
}

func (h *heap) push(s byte) {
	h.value = append(h.value, s)
}

func (h *heap) pop() (s byte) {
	s = h.value[len(h.value)-1]
	h.value = h.value[:len(h.value)-1]
	return s
}

func (h *heap) peak() (s byte) {
	return h.value[len(h.value)-1]
}
